/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.tree;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.TreeUnit;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;

/**
 * 
 * @Author Kent
 */
public class ServiceDomainTree extends Content implements TreeListener {
	private ServiceMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private TreeNodeContentListener listener = null;

	private GwtContext context = null;

	private Tree tree;

	private TreeItem selected;

	private ServiceDomainTreeItem localDomain, remoteDomain;

	public ServiceDomainTree(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context) {
		this.cons = cons;
		this.service = service;
		this.context = context;
		tree = new Tree();
		tree.setTitle(cons.serviceDomainTree());
		tree.addTreeListener(this);
		initWidget(tree);

	}

	public void setTreeNodeContentListener(TreeNodeContentListener l) {
		this.listener = l;

	}

	private AsyncCallback<ResponseObject> getAppCallback = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			for (int i = 0; i < ds.size(); i++) {
				TreeUnit st = (TreeUnit) ds.get(i);
				ServiceDomainTreeItem item = new ServiceDomainTreeItem();
				item.setHTML(st.getName());
				item.setId(st.getId());
				item.setKey(st.getKey());
				item.setParentId(st.getParentId());
				item.setType(st.getType());
				item.setSubType(st.getSubType());
				// item.addItem("");

				localDomain.addItem(item);

			}
			localDomain.setState(true, false);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	private AsyncCallback<ResponseObject> getRemoteCallback = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
			}

			ArrayList ds = (ArrayList) res.getResponse();

			for (int i = 0; i < ds.size(); i++) {
				TreeUnit st = (TreeUnit) ds.get(i);
				ServiceDomainTreeItem item = new ServiceDomainTreeItem();
				item.setHTML(st.getName());
				item.setId(st.getId());
				item.setKey(st.getKey());
				item.setParentId(st.getParentId());
				item.setType(st.getType());
				item.setSubType(st.getSubType());
				remoteDomain.addItem(item);

			}
			remoteDomain.setState(true, false);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public void onLoad() {
		ServiceDomainTreeItem root = new ServiceDomainTreeItem();
		root.setHTML(cons.serviceMgmt());
		root.setSelected(true);
		root.setSubType(ServiceConstants.SERVICE_DOMAIN_ROOT);

		selected = root;

		localDomain = new ServiceDomainTreeItem();
		localDomain.setHTML(cons.localDomain());
		localDomain.setSubType(ServiceConstants.SERVICE_DOMAIN_LOCAL);

		remoteDomain = new ServiceDomainTreeItem();
		remoteDomain.setHTML(cons.remoteDomain());
		remoteDomain.setSubType(ServiceConstants.SERVICE_DOMAIN_REMOTE);

		root.addItem(localDomain);
		root.addItem(remoteDomain);
		root.setState(true);

		tree.addItem(root);
		localDomain.addItem("");
		remoteDomain.addItem("");

	}

	public void onUnLoad() {
		tree = new Tree();
		selected = null;
	}

	public void onTreeItemSelected(TreeItem item) {
		if (!item.equals(selected))
			selected.setSelected(false);
		selected = item;
		this.listener.onTreeNodeSelected(item);

	}

	public void onTreeItemStateChanged(TreeItem item) {
		boolean open = item.getState();
		ServiceDomainTreeItem i = (ServiceDomainTreeItem) item;
		if (open) {
			// item.setState(true, false);
			if (i.getSubType().equals(ServiceConstants.SERVICE_DOMAIN_ROOT)) {
				// Window.alert("in " + i.getSubType() + " " + open);
				item.setState(true, false);
			} else if (i.getSubType().equals(
					ServiceConstants.SERVICE_DOMAIN_LOCAL)) {
				item.removeItems();
				ArrayList ds = (ArrayList) context
						.get(ServiceConstants.APP_LIST);
				for (int j = 0; j < ds.size(); j++) {
					KeyNamePairs st = (KeyNamePairs) ds.get(j);
					ServiceDomainTreeItem tmp = new ServiceDomainTreeItem();
					tmp.setHTML(st.getName());

					tmp.setKey(st.getId());
					tmp.setSubType(EntityConstants.TREE_APP_APP);
					localDomain.addItem(tmp);

				}

				// RequestObject req = new RequestObject();
				// req.setRequest(i.getId());
				// req
				// .setAction("han.bifang.gwt.server.action.app.GetAppTreeAction");
				// service.call(req, getAppCallback);
			} else if (i.getSubType().equals(
					ServiceConstants.SERVICE_DOMAIN_REMOTE)) {
				// Window.alert(i.getSubType() + " " + open);
				item.removeItems();
				ArrayList ds = (ArrayList) context
						.get(ServiceConstants.DOMAIN_LIST);
				for (int j = 0; j < ds.size(); j++) {
					KeyNamePairs st = (KeyNamePairs) ds.get(j);
					ServiceDomainTreeItem tmp = new ServiceDomainTreeItem();
					tmp.setHTML(st.getName());

					tmp.setKey(st.getId());
					tmp.setSubType(EntityConstants.TREE_DOMAIN_REMOTE);
					remoteDomain.addItem(tmp);

				}

				// RequestObject req = new RequestObject();
				// req.setRequest(i.getId());
				// req
				// .setAction("han.bifang.gwt.server.action.service.GetRemoteDomainAction");
				// service.call(req, getRemoteCallback);
			}
		} else {
			item.setState(false, false);

		}

	}

	public void init() {

	}

}
